package com.jair.crawk.main;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableBody;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;

public class Main2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WebClient webClient =  new WebClient(BrowserVersion.CHROME);//创建WebClient
		webClient.getOptions().setUseInsecureSSL(true);
		webClient.getOptions().setThrowExceptionOnScriptError(false);
		//webClient.setJavaScriptTimeout(10041);
		webClient.getOptions().setJavaScriptEnabled(false);
		webClient.getOptions().setCssEnabled(false);
		//webClient.getCookieManager().setCookiesEnabled(true);
		//webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		//webClient.getOptions().setTimeout(20000);
		try {
			String url = "https://cas.mioffice.cn/login?service=http%3A%2F%2Fopm.be.mi.com%2Fsqlquery%2Findex";
			String url2 = "https://cas.mioffice.cn/login";
			WebRequest request = new WebRequest(new URL(
					url));
			HtmlPage page = webClient.getPage(request);
			HtmlInput  userName = page.getElementByName("username");
			HtmlInput  pwd = page.getElementByName("password");
			userName.setValueAttribute("wenjie");
			pwd.setValueAttribute("1026wjpyzhxWJ");
			HtmlElement   button = (HtmlElement) page.getElementById("message_LOGIN_IMMEDIATELY");
	         HtmlPage page2 = button.click();
	         //判断是否成功
	         HtmlElement databseListDom = (HtmlElement) page2.getElementById("database-list");
	         Iterable<DomElement> ss = databseListDom.getChildElements();
	         for(DomElement e: ss){
	         }
	         
	         
	         HtmlInput dba = page2.getElementByName("SqlQuery[schema]");
	         dba.setValueAttribute("10.136.4.47:3334|bpm");
	         HtmlTextArea sql = page2.getElementByName("SqlQuery[sql]");
	         sql.setText("select * from `tb_asset_apply_record`");
	         HtmlElement Querybtn = (HtmlElement) page2.getElementById("processbutton");
	         HtmlPage ret = Querybtn.click();
	         DomNode node = ret.querySelector(".sql-result");
	         HtmlTable table =  (HtmlTable) node.getByXPath("//table").get(0);
	         //解析table
	         //FileWriter  file = new FileWriter("D://1.txt");
    	     List<HtmlTableRow> rows = table.getBodies().get(0).getRows();
    	   List<HtmlTableCell> cellList = table.getHeader().getRows().get(0).getCells();
    	   List<String> headerList = new ArrayList<String>();
    	   List<Map<String,String>> resultMap =  new ArrayList<Map<String,String>>();
    	    for(HtmlTableCell cell : cellList) {
    	    	 String columnName = cell.getTextContent();
    	    	 columnName = StringUtils.lowerCase(columnName);
    	    	 columnName = StringUtils.replace(columnName, " ", "_");
    	    	 headerList.add(columnName);
    	     }
	     	for (HtmlTableRow row : rows) {
	     		List<HtmlTableCell> cells = row.getCells();
	     		Map<String,String> map = new HashMap<String,String>();
	     		for (int i=0 ; i< cells.size() ;i++){
	     			map.put(headerList.get(i), cells.get(i).getTextContent());
	     			//file.write(' '+cell.getTextContent());
	     		}
	     		resultMap.add(map);
	     		//file.write('\n');
	     	}
	     	System.out.println(resultMap.size());
	         //file.flush();
	         //file.close();
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
